<template>
  <div class="chinese-character-dismantling">
    <div class="container">
      <h1>汉字拆字工具</h1>
      <el-form @submit.prevent="dismantleCharacter">
        <el-form-item label="输入汉字">
          <el-input 
            v-model="inputChar" 
            placeholder="请输入要拆解的汉字"
            maxlength="1"
          >
            <template #prefix>
              <el-icon><Edit /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <div class="button-group">
            <el-button type="primary" native-type="submit">
              <el-icon><Magnet /></el-icon>
              拆解汉字
            </el-button>
            <el-button type="warning" @click="goToHomepage">
              <el-icon><Back /></el-icon>
              返回首页
            </el-button>
          </div>
        </el-form-item>
      </el-form>
      <div class="result" v-if="displayChar">
        <span class="character">{{ displayChar }}</span>
        <template v-if="dismantleResult">
          <span>的拆解结果：</span>
          <span class="dismantled">{{ dismantleResult }}</span>
        </template>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Edit, Magnet, Back } from '@element-plus/icons-vue'
import { dismantleMap } from '@/data/characterDismantleData'

const router = useRouter()
const inputChar = ref('')
const displayChar = ref('')
const dismantleResult = ref('')

const dismantleCharacter = () => {
  if (!inputChar.value) {
    ElMessage.warning('请输入要拆解的汉字')
    return
  }
  if (inputChar.value.length > 1) {
    ElMessage.warning('请只输入一个汉字')
    return
  }
  displayChar.value = inputChar.value
  dismantleResult.value = dismantleMap[inputChar.value] || '暂无拆解数据'
}

const goToHomepage = () => {
  router.push('/')
}
</script>

<style lang="scss" scoped>
.chinese-character-dismantling {
  font-family: 'Roboto', Arial, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  margin: 0;
  padding: 40px 20px;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;

  .container {
    max-width: 800px;
    width: 100%;
    background-color: rgba(255, 255, 255, 0.95);
    padding: 40px;
    border-radius: 20px;
    box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);

    h1 {
      text-align: center;
      color: #4a5568;
      font-size: 2.5em;
      margin-bottom: 30px;
      text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    }

    .button-group {
      display: flex;
      justify-content: space-between;
      gap: 20px;

      .el-button {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;

        .el-icon {
          margin-right: 8px;
        }
      }
    }

    .result {
      margin-top: 30px;
      padding: 30px;
      background-color: #f7fafc;
      border-radius: 12px;
      text-align: center;
      box-shadow: inset 0 2px 4px rgba(0,0,0,0.05);

      .character {
        font-size: 36px;
        font-weight: bold;
        margin-right: 10px;
        color: #4a90e2;
      }

      .dismantled {
        font-size: 24px;
        color: #2c3e50;
      }
    }
  }
}

@media (max-width: 480px) {
  .chinese-character-dismantling {
    padding: 20px;

    .container {
      padding: 20px;

      h1 {
        font-size: 24px;
      }

      .button-group {
        flex-direction: column;
      }

      .result {
        padding: 20px;

        .character {
          font-size: 28px;
        }

        .dismantled {
          font-size: 20px;
        }
      }
    }
  }
}
</style>
